package org.corey.voxiverse;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;

public class Voxiverse {
	private World world;
	
	private void Run(){
		world = new World();
		
		while (!Display.isCloseRequested()){
			try{
				InputHandler.ProcessInput();
				GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
				GL11.glLoadIdentity();
				
				GL11.glTranslatef(-32f + Player.PX, -66f + Player.PY, -160f + Player.PZ);
				
				world.Render();
				
				Display.update();
				Display.sync(60);
				
			} catch (Exception e){
				
			}
		}
		
		
		
		Display.destroy();
	}
	
	public void Start() {
		try{
			CreateWindow();
			InitGL();
			
			Run();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	DisplayMode displayMode;
	
	private void CreateWindow() throws Exception {
		Display.setFullscreen(false);
		DisplayMode d[] = Display.getAvailableDisplayModes();
		for (int i = 0; i < d.length; i++) {
			if (d[i].getWidth() == 640 && d[i].getHeight() == 480
					&& d[i].getBitsPerPixel() == 32) {
				displayMode = d[i];
				break;
			}
		}
		Display.setDisplayMode(displayMode);
		Display.setTitle("LWJGL COMP4");
		Display.create();
	}
	
	private void InitGL() {
		GL11.glEnable(GL11.GL_TEXTURE_2D);
		GL11.glShadeModel(GL11.GL_SMOOTH);
		GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		GL11.glClearDepth(1.0);
		GL11.glEnable(GL11.GL_DEPTH_TEST);
		GL11.glDepthFunc(GL11.GL_LEQUAL);
		GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);

		GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);

		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();

		GLU.gluPerspective(45.0f, (float) displayMode.getWidth()
				/ (float) displayMode.getHeight(), 0.1f, 300.0f);
		GL11.glMatrixMode(GL11.GL_MODELVIEW);

		GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
	}
	
	
	
	public static void main(String[] args) throws LWJGLException {
		Voxiverse r = new Voxiverse();
		r.Start();
	}
	
	public enum GameState {
		State_Menu(0), State_SinglePlayer(1), State_Multiplayer(2), State_Editor(
				3);
		private int StateID;

		GameState(int i) {
			StateID = i;
		}

		public int GetID() {
			return StateID;
		}
	}
	
}
